package licode.unisop.client.feign.impl;

import licode.unisop.client.api.auth.AuthTokenShim;
import licode.unisop.client.api.auth.SopUserAuthQuery;
import licode.unisop.client.feign.SopUserAuthFeign;
import licode.unisop.client.info.SopTokenInfo;
import licode.unisop.client.utils.MethodCallUtil;
import licode.unisop.client.vo.SopAuthBrief;
import licode.unisop.client.vo.SopAuthUser;
import licode.unisop.client.vo.SopCredential;
import licode.unisop.client.vo.SopUserBrief;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author licode
 */
@Service
public class SopUserAuthQueryFeignImpl implements SopUserAuthQuery {
    @Resource
    private AuthTokenShim authTokenShim;

    @Resource
    private SopUserAuthFeign sopUserAuthFeign;

    private String getModalToken(String platformId) {
        return authTokenShim.modalToken(platformId);
    }

    @Override
    public SopAuthBrief getAuthBrief(SopTokenInfo tokenInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.getAuthBrief(
                        tokenInfo.getRealmId(),
                        getModalToken(tokenInfo.getToken()),
                        tokenInfo.getPlatformId(),
                        tokenInfo.getToken()));
    }

    @Override
    public SopAuthUser getAuthUserInfo(SopTokenInfo tokenInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.getAuthUserInfo(
                        tokenInfo.getRealmId(),
                        getModalToken(tokenInfo.getToken()),
                        tokenInfo.getPlatformId(),
                        tokenInfo.getToken()));
    }

    @Override
    public SopUserBrief getUserInfo(SopTokenInfo tokenInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.getUserInfo(
                        tokenInfo.getRealmId(),
                        getModalToken(tokenInfo.getToken()),
                        tokenInfo.getPlatformId(),
                        tokenInfo.getToken()));
    }

    @Override
    public SopCredential getCredential(SopTokenInfo tokenInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.getUserCredential(
                        tokenInfo.getRealmId(),
                        getModalToken(tokenInfo.getToken()),
                        tokenInfo.getPlatformId(),
                        tokenInfo.getToken()));
    }

    @Override
    public Map<String, Object> getUserAssets(SopTokenInfo tokenInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.getAuthUserAssets(tokenInfo.getRealmId(),
                        getModalToken(tokenInfo.getToken()),
                        tokenInfo.getPlatformId(),
                        tokenInfo.getToken()));
    }

    @Override
    public boolean tokenValid(SopTokenInfo tokenInfo) {
        return MethodCallUtil.execFeign(() ->
                sopUserAuthFeign.tokenValid(tokenInfo.getRealmId(),
                        getModalToken(tokenInfo.getToken()),
                        tokenInfo.getPlatformId(),
                        tokenInfo.getToken()));
    }
}
